/*
XBaseTools
Copyright (C) 2008 Matthew Perry

This library/program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;

namespace BaseTools.Strings
{
	/// <summary>
	/// A CharStream of a string
	/// </summary>>
	public class StringCharStream : ICharStream
	{
		string strData;
		int pos;
		
		/// <summary>
		/// Create a new StringCharStream
		/// </summary>
		/// <param name="s">The string to read from</param>
		public StringCharStream(string s)
		{
			strData = s;
			pos = 0;
		}
		
		/// <summary>
		/// Whether at end of string or not
		/// </summary>
		/// <returns></returns>
		public bool Available()
		{
			return pos < strData.Length;
		}
		
		/// <summary>
		/// Next character in the string
		/// </summary>
		/// <returns></returns>
		public char Next()
		{
			if (Available())
				return strData[pos++];
			else
				return '\0';
		}
		
		/// <summary>
		/// Cancels a read
		/// </summary>
		public void Unread()
		{
			if (pos > 0)
				pos--;
		}
		
		/// <summary>
		/// The string used by the stream
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return strData;
		}
	}
}
